﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CCOMDataAccess;

public partial class MemberEdit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (SessionManager.isLogged())
        {
            User loggedUser = UserManager.getUserInfos(int.Parse(Session["UserId"].ToString()));
            if (loggedUser.Rank1.AdminLevel > 99 || loggedUser.Matricule.ToString() == Request.Params["UserId"])
            {
                DataClassesDataContext db = new DataClassesDataContext();
                var query = from u in db.Users
                                      where u.Matricule == int.Parse(Request.Params["UserId"])
                                      select u;
                User user = query.First();

                #region Databinding...
                tbBirthDate.Text = user.BirthDate.ToShortDateString();
                tbEngagementDate.Text = user.EngagementDate.ToShortTimeString();
                tbExperience.Text = user.Experience.ToString();
                tbFirePower.Text = user.FirePower.ToString();
                tbMail.Text = user.Mail;
                tbName.Text = user.Name;
                tbPhoneNumber.Text = user.PhoneNumber;
                tbPrename.Text = user.Prename;
                tbPseudo.Text = user.Pseudo;
                rbRadio.SelectedIndex = user.Radio.Value;

                #region DropDown databinding...
                dlRank.DataSource = from r in db.Ranks
                                    select new
                                    {
                                        Id = r.Id,
                                        Name = r.Name
                                    };
                dlRank.DataTextField = "Name";
                dlRank.DataValueField = "Id";
                dlRank.DataBind();
                for (int i = 0; i < dlRank.Items.Count; i++)
                {
                    if (dlRank.Items[i].Value == user.Rank.Value.ToString())
                        dlRank.Items[i].Selected = true;
                }
                #endregion 
                #endregion

                #region Admin restrictions...
                if (loggedUser.Rank1.AdminLevel > 99)
                {
                    tbPseudo.Visible = true;
                    dlRank.Visible = true;
                    tbExperience.Visible = true;
                    tbEngagementDate.Visible = true;
                    tbFirePower.Visible = true;
                }
                else
                {
                    tbPseudo.Visible = false;
                    dlRank.Visible = false;
                    tbFirePower.Visible = false;
                    tbExperience.Visible = false;
                    tbEngagementDate.Visible = false;
                } 
                #endregion
            }
        }  
    }

    protected void bValid_Click(object sender, EventArgs e)
    {
        User loggedUser = UserManager.getUserInfos(SessionManager.getUserID());
        if (loggedUser.Rank1.AdminLevel > 99 || loggedUser.Matricule.ToString() == Request.Params["UserId"])
        {
            DataClassesDataContext db = new DataClassesDataContext();
            
            var query = from u in db.Users where u.Matricule == int.Parse(Request.Params["UserId"]) select u;
            User user = query.First();
            user.BirthDate = DateTime.Parse(tbBirthDate.Text);
            user.EngagementDate = DateTime.Parse(tbEngagementDate.Text);
            user.Equipment = tbEquipment.Text;
            user.Experience = int.Parse(tbExperience.Text);
            user.FirePower = int.Parse(tbFirePower.Text);
            user.Mail = tbMail.Text;
            user.Name = tbName.Text;
            user.PhoneNumber = tbPhoneNumber.Text;
            user.Prename = tbPrename.Text;
            user.Radio = int.Parse(rbRadio.SelectedValue);
            user.Rank = int.Parse(dlRank.SelectedValue);
            user.Pseudo = tbPseudo.Text;
            String toto = tbTest.Text;
            db.SubmitChanges();
            Response.Redirect("MemberView.aspx?UserId=" + Request.Params["UserId"]);
        }
    }

    protected void bReset_Click(object sender, EventArgs e)
    {
        Response.Redirect("CCOMWebsite/Members.aspx");
    }
}

